<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<%
    String username = "";
    String password = "";
    Cookie[] cookies = request.getCookies();
    for(Cookie cookie : cookies){
        // System.out.println(cookie.getName()+"="+cookie.getValue());
        if("username".equals(cookie.getName())){
            username = cookie.getValue();
        }
        if("password".equals(cookie.getName())){
            password = cookie.getValue();
        }
    }
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
    <base href="<%=basePath%>" />
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>新闻中国</title>
    <link href="css/read.css" rel="stylesheet" type="text/css" />
    <script type="text/javascript" src="/js/jquery-1.12.4.min.js"></script>
    <script type="text/javascript" src="/js/time.js"></script>
    <script type="text/javascript">
        $(function(){
            initNewsTop();
            initComments();
        });
        function deleteComment(cid){
            $.getJSON("/comment.do",{opr:"delete",cid:cid},function (result){
                if(result == true){
                    alert("删除成功！");
                    initComments(1);
                }else{
                    alert("删除失败！")
                }
            })
        }
        function initComments(pageNo){
            pageNo = pageNo || 1;
            $.getJSON("/comment.do",{opr:"getCommentsByCnid",cnid:$(":input[name='nid']").val(),pageNo:pageNo,pageSize:2},function(result){
                $(".commentTable").empty();
                $("#pageInfo").empty();
                if(result.count == 0){
                    $(".commentTable").append(
                        '<tr>' +
                        '<td colspan="6"> 暂无评论！ </td>' +
                        '</tr>' +
                        '<tr>' +
                        '<td colspan="6"><hr /></td>' +
                        '</tr>');
                }else{
                    console.log(result.list);
                    $(result.list).each(function(i,e){
                        $(".commentTable").append(
                            '<tr>'+
                            '<td> 留言人： </td>'+
                            '<td>'+e.cauthor+'</td>'+
                            '<td> IP： </td>'+
                            '<td>'+e.cip+'</td>'+
                            '<td> 留言时间： </td>'+
                            '<td>'+showDateTime(e.cdate)+'&nbsp;<button onclick="deleteComment('+e.cid+')">删除</button></td>'+
                            '</tr>'+
                            '<tr>'+
                            '<td colspan="6">'+e.ccontent+'</td>'+
                            '</tr>'+
                            '<tr>'+
                            '<td colspan="6"><hr />'+
                            '</td>'+
                            '</tr>');
                    });
                    // 显示分页信息
                    $("#pageInfo").append("<span>当前页数:["+result.pageNo+"/"+result.pageCount+"]&nbsp; 总数量："+result.count+"</span>&nbsp;&nbsp;");
                    if(result.pageNo > 1){
                        $("#pageInfo").append("<a href='javascript:initComments(1)'>首页</a>&nbsp;&nbsp;");
                        $("#pageInfo").append("<a href='javascript:initComments("+(result.pageNo-1)+")'>上一页</a>&nbsp;&nbsp;");
                    }
                    if(result.pageNo < result.pageCount){
                        $("#pageInfo").append("<a href='javascript:initComments("+(result.pageNo+1)+")'>下一页</a>&nbsp;&nbsp;");
                        $("#pageInfo").append("<a href='javascript:initComments("+result.pageCount+")'>尾页</a>&nbsp;&nbsp;");
                    }
                }
            })
        }
        function check(){
            var login_username = document.getElementById("uname");
            var login_password = document.getElementById("upwd");
            if(login_username.value == ""){
                alert("用户名不能为空！请重新填入！");
                login_username.focus();
                return false;
            }else if(login_password.value == ""){
                alert("密码不能为空！请重新填入！");
                login_password.focus();
                return false;
            }
            return true;
        }
        function checkComment(){
            var cauthor = document.getElementById("cauthor");
            var content = document.getElementById("ccontent");
            if(cauthor.value == ""){
                alert("用户名不能为空！！");
                return false;
            }else if(content.value == ""){
                alert("评论内容不能为空！！");
                return false;
            }
            // 发起ajax请求
            console.log($(".commentForm").serialize());// 普通字符串参数
            console.log($(".commentForm").serializeArray())// 适合文件上传
            $.post("/comment.do", $(".commentForm").serializeArray(),function(result){
                if(result == true){
                    alert("发表成功！");
                    initComments(1);
                    $(":input[name='ccontent']").val("");
                }else{
                    alert("发表失败！")
                }
            },"json")
            return false;
        }
        function toReadNews(nid){
            window.location.href = "/news.do?opr=read&nid="+nid;
        }
        // 左侧主题新闻
        function initNewsTop(){
            // 加载国内新闻
            $.getJSON("/news.do",{"opr":"getNewsByNtidTop","ntid":"1","pageSize":5},function(result){
                $("#guonei").empty();
                $(result.list).each(function(i,e){
                    $("#guonei").append("<li><a href='javascript:toReadNews("+e.nid+")'><b>"+e.ntitle+"</b></a></li>")
                })
            });
            // 加载国际新闻
            $.getJSON("/news.do",{"opr":"getNewsByNtidTop","ntid":"2","pageSize":5},function(result){
                $("#guoji").empty();
                $(result.list).each(function(i,e){
                    $("#guoji").append("<li><a href='javascript:toReadNews("+e.nid+")'><b>"+e.ntitle+"</b></a></li>")
                })
            });
            // 加载娱乐新闻
            $.getJSON("/news.do",{"opr":"getNewsByNtidTop","ntid":"5","pageSize":5},function(result){
                $("#yule").empty();
                $(result.list).each(function(i,e){
                    $("#yule").append("<li><a href='javascript:toReadNews("+e.nid+")'><b>"+e.ntitle+"</b></a></li>")
                })
            });
        }
    </script>
</head>
<body>
<div id="header">
    <div id="top_login">
        <c:choose>
            <c:when test="${not empty sessionScope.loginUser}"><!-- 已登录 -->
                <span>欢迎你：${sessionScope.loginUser.uname}</span>&nbsp;&nbsp;
                <span><a href="/user.do?opr=loginOut">注销</a></span>
            </c:when>
            <c:otherwise><!-- 未登录 -->
                <form action="/user.do" method="post" onsubmit="return check()">
                    <input type="hidden" name="opr" value="login"/>
                    <label> 登录名 </label>
                    <input type="text" id="uname" name="uname" value="<%=username%>" class="login_input" />
                    <label> 密&#160;&#160;码 </label>
                    <input type="password" id="upwd"  name="upwd" value="<%=password%>" class="login_input" />
                    <input type="checkbox" name="remmber" value="true" checked />记住密码
                    <input type="submit" class="login_sub" value="登录" />
                    <label id="error">${requestScope.loginError}</label>
                    <img src="images/friend_logo.gif" alt="Google" id="friend_logo" />
                </form>
            </c:otherwise>
        </c:choose>
    </div>
    <div id="nav">
        <div id="logo"> <img src="images/logo.jpg" alt="新闻中国" /> </div>
        <div id="a_b01"> <img src="images/a_b01.gif" alt="" /> </div>
        <!--mainnav end-->
    </div>
</div>
<div id="container">
    <%@ include file="../index-elements/index_sidebar.jsp" %>
    <div class="main">
        <div class="class_type"> <img src="images/class_type.gif" alt="新闻中心" /> </div>
        <div class="content">
            <ul class="classlist">
                <table width="80%" align="center">
                    <tr width="100%">
                        <td colspan="2" align="center">${news.ntitle}</td>
                    </tr>
                    <tr>
                        <td colspan="2"><hr />
                        </td>
                    </tr>
                    <tr>
                        <td align="center">作者：&nbsp;&nbsp;${news.nauthor}&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                        </td>
                        <td align="left">发布时间：${news.ncreateDate} </td>
                    </tr>
                    <tr>
                        <td colspan="2" align="center"></td>
                    </tr>
                    <tr>
                        <td colspan="2">${news.ncontent}</td>
                    </tr>
                    <tr>
                        <td colspan="2"><hr />
                        </td>
                    </tr>
                </table>
            </ul>
            <ul class="classlist">
                <table class="commentTable" width="80%" align="center">
                    <%-- <c:choose>
                     <c:when test="${empty news.comments}">
                         <tr><td colspan="6"> 暂无评论！ </td></tr>
                         <tr>
                             <td colspan="6"><hr />
                             </td>
                         </tr>
                     </c:when>
                     <c:otherwise>
                         <c:forEach items="${news.comments}" var="comment">
                             <tr>
                                 <td> 留言人： </td>
                                 <td>${comment.cauthor}</td>
                                 <td> IP： </td>
                                 <td>${comment.cip}</td>
                                 <td> 留言时间： </td>
                                 <td><fmt:formatDate value="${comment.cdate}" pattern="yyyy-MM-dd HH:mm:ss" /></td>
                             </tr>
                             <tr>
                                 <td colspan="6">${comment.ccontent}</td>
                             </tr>
                             <tr>
                                 <td colspan="6"><hr />
                                 </td>
                             </tr>
                         </c:forEach>
                     </c:otherwise>
                     </c:choose>--%>
                </table>
            </ul>
            <p id="pageInfo"></p>
            <ul class="classlist">
                <form class="commentForm" action="util/news?opr=addComment" method="post" onSubmit="return checkComment()">
                    <input type="hidden" name="opr" value="add" />
                    <input type="hidden" name="nid" value="${news.nid}" />
                    <table width="80%" align="center">
                        <tr>
                            <td> 评 论 </td>
                        </tr>
                        <tr>
                            <td> 用户名： </td>
                            <td>
                                <c:choose>
                                    <c:when test="${not empty sessionScope.loginUser}">
                                        <input id="cauthor" name="cauthor" value="${sessionScope.loginUser.uname}" readonly="readonly" style="border:0px;"/>
                                    </c:when>
                                    <c:otherwise>
                                        <input id="cauthor" name="cauthor" value="匿名"/>
                                    </c:otherwise>
                                </c:choose>
                                IP：
                                <input name="cip" id="cip" value="${pageContext.request.remoteAddr}" readonly="readonly" style="border:0px;"/>
                            </td>
                        </tr>
                        <tr>
                            <td colspan="2"><textarea name="ccontent" id="ccontent" cols="70" rows="10"></textarea>
                            </td>
                        </tr>
                        <tr><td><input name="submit" value="发  表" type="submit"/>
                        </td></tr>
                    </table>
                </form>
            </ul>
        </div>
    </div>
</div>
<%@ include file="../index-elements/index_bottom.html"%>
</body>
</html>
